package com.bachongLightNovel.system.controller;


import com.bachongLightNovel.model.dto.system.PasswordDto;
import com.bachongLightNovel.model.dto.system.SystemUserAssignDto;
import com.bachongLightNovel.model.dto.system.SystemUserPageDto;
import com.bachongLightNovel.model.pojo.system.User;
import com.bachongLightNovel.model.vo.page.PageVo;
import com.bachongLightNovel.model.vo.system.UserInfoVo;
import com.bachongLightNovel.result.Result;
import com.bachongLightNovel.system.service.SystemUserService;
import com.bachongLightNovel.model.dto.system.LoginDto;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/system/user")
@Slf4j
public class SystemUserController {


    @Autowired
//    @Resource
    private SystemUserService systemUserService;


    /**
     * 用户登录方法
     * @return
     */
    @PostMapping("/login")
    public Result<String> login(@RequestBody LoginDto dto){
        log.info("登录接口");
        String token=systemUserService.login(dto);
        return Result.success(token);
    }

    /**
     * 从请求头中获取登录用户的id，并获取用户信息
     * @param userId
     * @return
     */
    @GetMapping("/userinfo")
    public Result getUserInfo(@RequestHeader(value = "userId") Long userId){
//        String str=systemUserService.getUserInfo(userId);
        UserInfoVo vo=systemUserService.getUserInfo(userId);
        return Result.success(vo);
    }

    /**
     * 用户条件分页列表查询
     * @param current
     * @param size
     * @param dto
     * @return
     */
    @PostMapping("/findPage/{current}/{size}")
    public Result findPage(@PathVariable Integer current,
                           @PathVariable Integer size,
                           @RequestBody SystemUserPageDto dto){
        log.info("系统管理用户条件分页列表查询");
        PageVo<User> vo=systemUserService.findPage(current,size,dto);
        return Result.success(vo);
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping("/save")
    public Result saveUser(@RequestBody User user){
        log.info("添加用户");
        systemUserService.saveUser(user);
        return Result.success();
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping("/update")
    public Result updateUser(@RequestBody User user){
        log.info("修改用户信息");
        systemUserService.updateUser(user);
        return Result.success();
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    public Result removeById(@PathVariable Long id){
        log.info("根据id删除用户：{}",id);
        systemUserService.removeById(id);
        return Result.success();
    }

    /**
     * 为用户分配角色
     * @param dto
     * @return
     */
    @PostMapping("/assign")
    public Result assign(@RequestBody SystemUserAssignDto dto){
        log.info("为用户分配角色：{}",dto);
        systemUserService.assign(dto);
        return Result.success();
    }

    /**
     * 退出登录
     * @param :
     * @return Result
     * @author vermouth
     */
    @GetMapping("/logout")
    public Result logout(@RequestHeader("Token") String token){
        log.info("退出登录：{}",token);
        systemUserService.logout(token);
        return Result.success();
    }

    /**
     * 修改密码
     * @param dto:
     * @return Result
     * @author vermouth
     */
    @PostMapping("update-password")
    public Result updatePassword(@RequestBody PasswordDto dto){
        log.info("修改密码：{}",dto);
        systemUserService.updatePassword(dto);
        return Result.success();
    }

    /**
     * 注册用户
     * @param user:
     * @return Result
     * @author vermouth
     */
    @PostMapping("register")
    public Result register(@RequestBody User user){
        log.info("注册用户：{}",user);
        systemUserService.saveUser(user);
        return Result.success();
    }
}
